﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.Enums;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Utility;
using Sector7.Framework.Core;
using Sector7.Framework.DataDictionary.Agents;
using Sector7.Framework.Consts;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// DataTemplateSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataTemplateSummaryWindow : UserControl
    {
        #region Private Functions
        /// <summary>
        /// 页面加载时读取数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataTemplateSummaryWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new System.ComponentModel.DoWorkEventHandler(LoadData_DoWork);
            worker.RunWorkerAsync();

            btnEditDataTemplate.IsEnabled = false;
        }

        /// <summary>
        /// 加载数据工作线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadData_DoWork(object obj, System.ComponentModel.DoWorkEventArgs e)
        {
            GetData();
            this.Dispatcher.BeginInvoke(new DelegateWithNothing(LoadData));
        }

        /// <summary>
        /// 获取所有模板信息
        /// </summary>
        private void GetData()
        {
            m_dataTemplates = DataTemplateServiceAgent.GetAll();
        }

        private void LoadData()
        {
            ClientUtil.LoadItemsSource(dtgMain, m_dataTemplates);
        }

        /// <summary>
        /// 添加数据模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddDataTemplate_Click(object sender, RoutedEventArgs e)
        {
            AddDataTemplateWindow addDataTemplateWindow = new AddDataTemplateWindow();
            addDataTemplateWindow.InitializeWindow(CrudType.Create);
            addDataTemplateWindow.AddSuccess += new EventHandler(addDataTemplateWindow_AddSuccess);

            addDataTemplateWindow.ShowDialog();
        }

        void addDataTemplateWindow_AddSuccess(object sender, EventArgs e)
        {
            var addWindow = sender as AddDataTemplateWindow;
            var newDataTemplate = addWindow.DataTemplate;

            int optResult = DataTemplateServiceAgent.Add(newDataTemplate);

            newDataTemplate.TemplateId = Convert.ToInt32(optResult);

            m_dataTemplates.Add(newDataTemplate);

            ClientUtil.LoadSelectedItem(dtgMain, newDataTemplate);

            addWindow.ResetWindow();
        }

        /// <summary>
        /// 数据选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dtgMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedDataTemplate = dtgMain.SelectedItem as S7_DataTemplate;

            if (selectedDataTemplate != null)
            {
                ClientUtil.LoadItemsSource(dtgItems, selectedDataTemplate.Items);
            }
        }

        /// <summary>
        /// 删除数据模板
        /// </summary>
        private void DoDelete()
        {
            var selectedDataTemplate = dtgMain.SelectedItem as S7_DataTemplate;

            if (selectedDataTemplate != null)
            {
                MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "是否确认删除选中项？");
                if (result == MessageBoxResult.Yes)
                {
                    int optResult = DataTemplateServiceAgent.DeleteDataTemplate(selectedDataTemplate.TemplateId);

                    if (optResult == C_OperatorResultType.SUCCESS)
                    {
                        Alert.ShowDialog(AlertType.Notification, "删除数据模板成功!");
                        m_dataTemplates.Remove(selectedDataTemplate);
                        dtgMain.Items.Refresh();
                    }
                    else
                    {
                        Alert.ShowDialog(AlertType.Notification, "删除数据模板失败!");
                    }
                }
            }
        }

        private void btnDelDataTemplate_Click(object obj, RoutedEventArgs e)
        {
            DoDelete();
        }
        #endregion

        #region Structure
        public DataTemplateSummaryWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner Member
        /// <summary>
        /// 获取所有数据模板信息列表
        /// </summary>
        private List<S7_DataTemplate> m_dataTemplates = null;
        #endregion
    }
}
